package com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.entity.ErpFinancepayment;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.entity.ErpFinancepaymentbill;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.mapper.ErpFinancepaymentbillMapper;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.dto.ErpFinancepaymentListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.mapper.ErpFinancepaymentMapper;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.service.IErpFinancepaymentService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-09-24
 */
@Service
@AllArgsConstructor
public class ErpFinancepaymentServiceImpl extends BaseService<ErpFinancepaymentMapper, ErpFinancepayment> implements IErpFinancepaymentService {

	private ErpFinancepaymentbillMapper erpFinancepaymentbillMapper;

	@Override
	public IPage<ErpFinancepayment> getPageList(ErpFinancepaymentListDto pageListDto) {
		Wrapper<ErpFinancepayment> wrapper = Wrappers.<ErpFinancepayment>query().lambda()				.eq(!StringUtil.isEmpty(pageListDto.getF_PaymentCode()), ErpFinancepayment::getFPaymentcode, pageListDto.getF_PaymentCode())
				.like(!StringUtil.isEmpty(pageListDto.getF_Manager()), ErpFinancepayment::getFManager, pageListDto.getF_Manager())
				.eq(!StringUtil.isEmpty(pageListDto.getF_PurchaseOrderId()), ErpFinancepayment::getFPurchaseorderid, pageListDto.getF_PurchaseOrderId())
				.like(!StringUtil.isEmpty(pageListDto.getF_DeliveryUnit()), ErpFinancepayment::getFDeliveryunit, pageListDto.getF_DeliveryUnit())
				.like(!StringUtil.isEmpty(pageListDto.getF_PurchaseMan()), ErpFinancepayment::getFPurchaseman, pageListDto.getF_PurchaseMan());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addErpFinancepayment(ErpFinancepayment erpFinancepayment, List<ErpFinancepaymentbill> erpFinancepaymentbillList) {
		String erpFinancepaymentId = IdWorker.get32UUID();
		erpFinancepayment.setFId(erpFinancepaymentId);
		erpFinancepaymentbillList.forEach(erpFinancepaymentbill -> erpFinancepaymentbill.setFPurchaseorderid(erpFinancepaymentId));
		this.saveBatch(erpFinancepaymentbillList, ErpFinancepaymentbill.class, ErpFinancepaymentbillMapper.class);
		boolean isSuccess = this.save(erpFinancepayment);
		return isSuccess ? erpFinancepaymentId : null;
	}

	@Override
	public boolean updateErpFinancepayment(String id, ErpFinancepayment erpFinancepayment, List<ErpFinancepaymentbill> erpFinancepaymentbillList) {
		erpFinancepayment.setFId(id);
		erpFinancepaymentbillList.forEach(erpFinancepaymentbill -> erpFinancepaymentbill.setFPurchaseorderid(id));
		erpFinancepaymentbillMapper.delete(Wrappers.<ErpFinancepaymentbill>query().lambda().eq(ErpFinancepaymentbill::getFPurchaseorderid, id));
		this.saveBatch(erpFinancepaymentbillList, ErpFinancepaymentbill.class, ErpFinancepaymentbillMapper.class);
		return this.updateById(erpFinancepayment);
	}

	public List<ErpFinancepaymentbill> getErpFinancepaymentbillByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpFinancepaymentbill>query().lambda().eq(ErpFinancepaymentbill::getFPurchaseorderid, parentId);
		return erpFinancepaymentbillMapper.selectList(wrapper);
	}
}
